//
//  CleanCacheController.m
//  QuickCure1
//
//  Created by 李江波 on 2016/11/4.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "CleanCacheController.h"

#import "ProgressManager.h"

#define CachePath  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]

@interface CleanCacheController ()

@property (weak, nonatomic) IBOutlet UILabel *cacheSize;
@property(nonatomic ,assign) NSInteger totalSize;
@property(nonatomic,assign)CGFloat number;
@property(nonatomic,weak)UIProgressView* pro1;

@end

@implementation CleanCacheController

- (void)viewDidLoad {
    [super viewDidLoad];
     [self setupUI];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[self OriginImage:[UIImage imageNamed:@"Default"] scaleToSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT)]];
    
    
    [ProgressManager  alertInfoStatus:@"正在计算缓存"];
    
    // 获取文件夹尺寸
    // 文件夹非常小,如果我的文件非常大
  
    [XMGFileTool getFileSize:CachePath completion:^(NSInteger totalSize) {
        
        _totalSize = totalSize;
      
        [ProgressManager dismissLoading];
        self.cacheSize.textColor = [UIColor whiteColor];
        
        self.cacheSize.text = [self sizeStr];
    }];

   
}
#pragma mark
#pragma mark - 搭建UI界面
- (void)setupUI
{
    //实例化一个进度条，有两种样式，一种是UIProgressViewStyleBar一种是UIProgressViewStyleDefault，几乎无区别
    UIProgressView *pro1=[[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    self.pro1 = pro1;
    
    //设置的高度对进度条的高度没影响，整个高度=进度条的高度，进度条也是个圆角矩形
    //但slider滑动控件：设置的高度对slider也没影响，但整个高度=设置的高度，可以设置背景来检验
    
    
    pro1.frame=CGRectMake(110, 500, 200, 50);
    //设置进度条颜色
    pro1.trackTintColor=[UIColor blackColor];
    //设置进度默认值，这个相当于百分比，范围在0~1之间，不可以设置最大最小值
   
    pro1.progress=0.7;
    //设置进度条上进度的颜色
    pro1.progressTintColor=[UIColor redColor];
    //设置进度条的背景图片
//    pro1.trackImage=[UIImage imageNamed:@"logo.png"];
//    //设置进度条上进度的背景图片
//    pro1.progressImage=[UIImage imageNamed:@"1.png"];
    //设置进度值并动画显示
    if (!self.number) {
        self.number = 0.7;
    }
    [pro1 setProgress:self.number animated:YES];
 
    
    [self.view addSubview:pro1];
    

    
}
#pragma mark
#pragma mark - 拉伸背景图片,适应屏幕
-(UIImage *)OriginImage:(UIImage *)image scaleToSize:(CGSize)size{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

- (IBAction)cleanCache:(id)sender {
    
    // 清空缓存
    // 删除文件夹里面所有文件
    [XMGFileTool removeDirectoryPath:CachePath];
    
    _totalSize = 0;
    
    self.cacheSize.text = @"已经清理完毕了哦😁";
    
    [self.pro1 setProgress:0 animated:YES];

}

// 获取缓存尺寸字符串
- (NSString *)sizeStr
{
    NSInteger totalSize = _totalSize;
    NSString *sizeStr = @"清除缓存";
    // MB KB B
    if (totalSize > 1000 * 1000) {
        // MB
        CGFloat sizeF = totalSize / 1000.0 / 1000.0;
        sizeStr = [NSString stringWithFormat:@"%@(%.1fMB)",sizeStr,sizeF];
    } else if (totalSize > 1000) {
        // KB
        CGFloat sizeF = totalSize / 1000.0;
        sizeStr = [NSString stringWithFormat:@"%@(%.1fKB)",sizeStr,sizeF];
    } else if (totalSize > 0) {
        // B
        sizeStr = [NSString stringWithFormat:@"%@(%.ldB)",sizeStr,totalSize];
    }
    
    return sizeStr;
}



@end
